package cn.edu.gdut.deepcode.controller;


import cn.edu.gdut.deepcode.domain.dto.UserDTO;
import cn.edu.gdut.deepcode.domain.po.Result;
import cn.edu.gdut.deepcode.domain.po.User;
import cn.edu.gdut.deepcode.service.IUserService;
import cn.edu.gdut.deepcode.context.UserContext;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import static cn.edu.gdut.deepcode.constant.AbnormalInfoConstant.UL_USER_EI_1;

/**
 * <p>
 * 用户表前端控制器
 * </p>
 *
 * @Author: 暮倦
 * @Since: 2025-04-08  21:04
 * @Version: 1.0.0
 */
@RestController
@RequestMapping("/api/users")
@Slf4j
@RequiredArgsConstructor
public class UserController {
    private final IUserService userService;
    @PutMapping("/theme")
    public Result<?> updateTheme(@RequestBody UserDTO userDTO){
        Long userId = UserContext.getUser();
        // 参数校验
        if (BeanUtil.isEmpty(userDTO)|| StrUtil.isBlank(userDTO.getTheme())){
            log.error(UL_USER_EI_1, List.of(userId,userDTO,userDTO.getTheme()));
            return Result.error();
        }
        // 更新主题
        userService.lambdaUpdate()
                .eq(User::getId, userId)
                .set(User::getTheme,userDTO.getTheme())
                .update();
        return Result.success();
    }
}
